#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 16:33:53 2018
查看dmrg历史上最多用了多少MiB内存
@author: bb
"""
import psutil
import time


def get_used_memory(task_name):
    for id in psutil.pids():
        p = psutil.Process(id)
        if (p.name() == task_name):
            mem = p.memory_info()[0] / float(2**20)  #把单位从byte转换为MiB
            return mem
    return 0


record = 0
while True:
    try:
        mem = get_used_memory('dmrg')
        if mem>record:
            record=mem
    except:
        time.sleep(1)
    print('the task is using memory %f MiB, max history %f' % (mem,
                                                           record))
    time.sleep(0.5)

